package com.billsharing.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.PreparedStatement;

public class User {

	private int uid;
	private String uname = "";
	private String email = "";
	private Map<String, Double> creditInfo = new HashMap<String, Double>();
	private Map<String, Double> debitInfo = new HashMap<String, Double>();
	private ArrayList<String> friendsList = new ArrayList<String>();
	public ArrayList<String> friendsList1 = new ArrayList<String>();
	User(int userId) {

		try {
			DbConnect dbconn = new DbConnect();
			Connection conn;
			conn = dbconn.getConnection();
			Statement s1 = conn.createStatement();
			s1.executeQuery("select * from Users where uid =" + userId);
			ResultSet rs1 = s1.getResultSet();
			if (rs1.next()) {
				uname = rs1.getString("username");
				uid = rs1.getInt("uid");
				email = rs1.getString("emailid");
				
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

	User(String emailId) {
		try {
			DbConnect dbconn = new DbConnect();
			Connection conn;
			conn = dbconn.getConnection();
			Statement s1 = conn.createStatement();
			s1.executeQuery("select * from Users where emailid='"+emailId+"'");
			ResultSet rs1 = s1.getResultSet();
			if (rs1.next()) {
				uname = rs1.getString("username");
				uid = rs1.getInt("uid");
				email = rs1.getString("emailid");
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	void setUserName(String userName) {
		uname = userName;
	}

	int getUid() {
		return uid;
	}

	String getUserName() {
		return uname;
	}
/**
 * returns the email id of the current user
 * 
 * @param  
 * @return email String
 */
	String getEmail()
	{
		return email;
	}
	
	boolean addFriend(String email) {
		boolean result = false;
		String email1;	
		DbConnect dbconn = new DbConnect();
		Connection conn = dbconn.getConnection();
		try {
			Statement s = conn.createStatement();
			Statement s1 = conn.createStatement();
			Statement s2=conn.createStatement();
			Statement s3=conn.createStatement();
			
			System.out.println("Email id is" + email);
			s.executeQuery("select uid,emailid from Users where emailid='"+email+"'");
			
			ResultSet rs = s.getResultSet();
			
			while (rs.next()) {
				int fndid, phoneno;
				String fndid1 = rs.getString("uid");
				email = rs.getString("emailid");
				fndid=Integer.parseInt(fndid1);
				System.out.println(fndid1 + email);
				s2.executeQuery("select emailid from Users where uid="+uid);
				ResultSet rs2=s2.getResultSet();
				int i = s1.executeUpdate("insert into Friend (userid,Friendid,fndemail,credit,debit) values ("+ uid+ ","+ fndid	+ ",'"+ email+ "',"+0+","+0+")");
				if (i > 0) 
				{
					while(rs2.next())
					{
						email1=rs2.getString("emailid");
					
						
						int i1 = s3.executeUpdate("insert into Friend (userid,Friendid,fndemail,credit,debit) values ("+fndid+","+uid+",'"+email1+"',"+0+","+0+")");
					
					if(i1>0)
					{
						result= true;
						break;
					}
					}
				}
				else {
					result = false;
				}
			}
			
			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
System.out.println(result);
		return result;
	}


	boolean addGroup(String grpname,int userid) {
		boolean result = false;
		
		DbConnect dbconn = new DbConnect();
		Connection conn = dbconn.getConnection();
		try {
			Statement s = conn.createStatement();
			int i= s.executeUpdate("insert into Groups (groupname,groupmemid) values ('"+grpname+"',"+userid);
			if(i>0)
				result=true;
			else
				result=false;
			}
		catch(Exception e)
		{
			
		}
		return result;
	}
	
/**
 * Gives the list of all friends of the user
 * @param 
 * @return list of all ArrayList of all user objects of friends to the current user
 */
	ArrayList<String> getFriendsList()
	{

		DbConnect dbconn = new DbConnect();
		Connection conn = dbconn.getConnection();
		try {

			Statement s = conn.createStatement();
			s.executeQuery("select * from Friend where userid=" + uid);
			ResultSet rs = s.getResultSet();

			while (rs.next()) {

				String email = rs.getString("fndemail");


				friendsList.add(email);
				}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return friendsList;
	}
	Map<String, Double> getCreditInfo() {
		Connection conn;

		int userId = 0;
		Double credit = null;
		User user = null;

		DbConnect dbconn = new DbConnect();
		conn = dbconn.getConnection();
		try {

			Statement s = conn.createStatement();
			s.executeQuery("select * from Friend where userid=" + uid);
			ResultSet rs = s.getResultSet();

			while (rs.next()) {
				userId = rs.getInt("Friendid");
				credit = rs.getDouble("credit");
				user = new User(userId);
					creditInfo.put(user.getEmail(), credit);

			

			}

		} catch (Exception e) {
			e.printStackTrace();

		}

		return creditInfo;
	}

	Map<String, Double> getDebitInfo() {
		Connection conn;
		int userId = 0;
		Double credit = null;
		User user = null;
		Double debit = null;

		DbConnect dbconn = new DbConnect();
		conn = dbconn.getConnection();
		try {

			Statement s = conn.createStatement();
			s.executeQuery("select * from Friend where userid=" + uid);
			ResultSet rs = s.getResultSet();

			while (rs.next()) {
				userId = rs.getInt("Friendid");
				credit = rs.getDouble("credit");
				debit = rs.getDouble("debit");
				user = new User(userId);

					debitInfo.put(user.getEmail(), debit);


			}

		} catch (Exception e) {
			e.printStackTrace();

		}

		return debitInfo;
	}
	
	ArrayList<String> getFriends()
	{
		try
		{
		DbConnect dbconn = new DbConnect();
		Connection conn;
		String result,emaillist;
		conn = dbconn.getConnection();
		Statement s1 = conn.createStatement();
		s1.executeQuery("select fndemail from Friend where userid =" + uid);
		ResultSet rs1 = s1.getResultSet();
		if (rs1.next()) 
		{
			emaillist = rs1.getString("fndemail");
			friendsList1.add(emaillist);
		}
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return friendsList1;
	}
	
/**
 * reports a payment for the amount specified to the user identified by the email
 * @param email
 * @param amount
 */
	void reportPayment(String email, double amount)
	{
		double presentDebt = 0,netDebt;
		DbConnect dbconn = new DbConnect();
		Connection conn;
		conn = dbconn.getConnection();
		String email1=email;
		try
		{
		Statement s = conn.createStatement();
		System.out.println("USer id is"+uid);
		System.out.println("Emaild  id is"+email);
		s.executeQuery("select debit from Friend where userid="+uid+" and fndemail='"+email1+"'");
		ResultSet rs = s.getResultSet();
		while(rs.next())
		{
			System.out.println("Inside rffesult set");
			
			presentDebt = rs.getDouble("debit");
		}
		System.out.println(presentDebt);
		
		netDebt = presentDebt - amount;
		Statement s1 = conn.createStatement();
		System.out.println(netDebt);
		System.out.println(amount);
		String sql = "update Friend set debit=? WHERE userid =? AND fndemail=?";
		PreparedStatement update;
		update = conn.prepareStatement(sql);
		update.setDouble(1,netDebt);
		update.setInt(2, uid);
		update.setString(3, email1);
		int i = update.executeUpdate();
		if (i>0)
		{
			System.out.println("sucess");
		}
		else
		{
			System.out.println("fail");
		}
		
		User payer = new User(uid);
		String payerEmail = payer.getEmail();
		User recevier = new User(email1);
		int recevierUid = recevier.getUid();
		String reverseSql = "update Friend set credit=? WHERE userid =? AND fndemail=?";
		PreparedStatement reverseUpdate;
		reverseUpdate = conn.prepareStatement(reverseSql);
		reverseUpdate.setDouble(1,netDebt);
		reverseUpdate.setInt(2, recevierUid);
		reverseUpdate.setString(3,payerEmail);
		int j = reverseUpdate.executeUpdate();
		if (j>0)
		{
			System.out.println("sucess");
		}
		else
		{
			System.out.println("fail");
		}

		}
		catch(Exception e)
		{
			System.out.println(e.toString());	
		e.printStackTrace();
		}
	}
}
